package com.digital.herbarium.Controller;
import com.digital.herbarium.Entity.Atlas;
import com.digital.herbarium.annotation.CheckToken;
import com.digital.herbarium.Entity.ResponseResult;
import com.digital.herbarium.annotation.Log;
import com.digital.herbarium.service.AtlasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/atlas")
public class AtlasController {

    @Autowired
    AtlasService atlasService;
    @Value("${static.excelurl}")
    private String excelurl;


    @GetMapping("getAtlasInfo")
    @Log("atlas")
    public ResponseResult getAtlasInfo(Atlas atlas, @RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize,
                                       @RequestParam(value = "pageNum",defaultValue = "1",required = false) int pageNum){
        return ResponseResult.SUCCESS().setData( atlasService.createList(atlas,pageSize,pageNum));
    }


    @GetMapping("getManagerAtlasInfo")
    @CheckToken()
    @Log("atlas")
    public ResponseResult getManagerAtlasInfo(Atlas atlas,@RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize,
                                               @RequestParam(value = "pageNum",defaultValue = "0",required = false) int pageNum){
        return ResponseResult.SUCCESS().setData( atlasService.getAtlasInfo(atlas,pageSize,pageNum));
    }


    @PostMapping("addAtlasInfo")
    @CheckToken()
    public  ResponseResult addAtlasInfo(Atlas  atlas){
        atlasService.addAtlasInfo(atlas);
        return ResponseResult.SUCCESS().setData("添加成功");
    }

    @PostMapping("editAtlasInfo")
    @CheckToken()
    public  ResponseResult editAtlasInfo(Atlas atlas){
        atlasService.editAtlasInfo(atlas);
        return ResponseResult.SUCCESS().setData("更新成功");
    }

    @GetMapping("delAtlasInfo")
    @CheckToken()
    public ResponseResult delAtlasInfo(String id){
        atlasService.delAtlasInfo(id);
        return  ResponseResult.SUCCESS().setData("删除成功");
    }


    @PostMapping("uploadExcel")
    @CheckToken()
    public  ResponseResult uploadExcel(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request){
        atlasService.addExcel(file,request,excelurl);
        return ResponseResult.SUCCESS().setData("上传成功");
    }
}
